<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入专业名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="课程名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入课程名" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="课程代码" prop="code">
            <el-input v-model="queryParams.code" placeholder="请输入课程代码" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['system:subject:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
              v-hasPermi="['system:subject:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['system:subject:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['system:subject:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="subjectList" @selection-change="handleSelectionChange">

          <el-table-column prop="picUrl" label="封面图" width="120" align="center">
            <template slot-scope="scope">
              <img :src="bindPic(scope.row.picUrl)" class="dept-icon" style="width: 80px;height: 53px;">
            </template>
          </el-table-column>

          <el-table-column prop="courseId" label="专业" width="180" align="center">
            <template slot-scope="scope">
              {{ bindCourse(scope.row.courseId) }}
            </template>
          </el-table-column>
          <el-table-column label="课程" align="center" prop="name" />
          <el-table-column label="代码" align="center" prop="code" />
          <!-- <el-table-column label="产品库存" align="center" prop="stock" /> -->
          <el-table-column label="产品价格" align="center" prop="price" />
          <el-table-column prop="type" label="课程类型" width="80">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.type == 1">线上课程</el-tag>
              <el-tag type="success" v-if="scope.row.type == 2">实体课程</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="描述" align="center" prop="subName" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-document" @click="other(scope.row)"
                v-hasPermi="['system:subject:edit']">更多操作</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['system:subject:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:subject:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>


    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="课程名" prop="name">
              <el-input v-model="form.name" placeholder="请输入课程产品名" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="专业" prop="courseId">
              <treeselect v-model="form.courseId" :options="deptOptions" :show-count="true" placeholder="请选择专业" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课程代码" prop="code">
              <el-input v-model="form.code" placeholder="请输入课程代码" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="课程描述" prop="subName">
          <el-input v-model="form.subName" type="textarea" placeholder="请输入简短描述" />
        </el-form-item>


        <el-form-item label="封面图" prop="picUrl">
          <div>
            <uploadpic />
          </div>

        </el-form-item>
        <el-form-item label="详情图" prop="detailUrl">

        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品属性" prop="attribute">
              <el-input v-model="form.attribute" placeholder="请输入产品属性" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="产品库存" prop="stock">
              <el-input v-model="form.stock" placeholder="请输入产品库存" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="价格" prop="price">
              <el-input placeholder="请输入课程价格" v-model="form.price">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="失效时间" prop="endTime">
              <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择失效时间"
                style="width: 170px;">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="模拟销量" prop="sellNum">
              <el-input v-model="form.sellNum" placeholder="请输入销量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="课程类型">
            <el-radio-group v-model="form.type">
              <el-radio v-for="dict in dict.type.subjec_type" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>


        <el-form-item label="课程详情" prop="productDetail">
          <editor v-model="form.productDetail" :min-height="220" />

        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 任务抽屉 -->
    <el-drawer :visible.sync="drawer" :direction="direction"   size="78%"  :title="title" >
      <div style="padding: 16px; width: 100%;">

        <el-tabs v-model="activeName" @tab-click="handleClick" style="margin-top: -20px;">

          <el-tab-pane label="章节管理" name="0">
            <chapter :deptId ="clickDeptId"  :subId ="clickSubId" v-if="drawer"/>
          </el-tab-pane>
          <el-tab-pane label="题库管理" name="1">
            <bank  :deptId ="clickDeptId"    :subId ="clickSubId"  v-if="drawer"/>
          </el-tab-pane>
          <el-tab-pane label="任务管理" name="2">
            <task  :deptId ="clickDeptId"   :subId ="clickSubId"  v-if="drawer" />
          </el-tab-pane>
          <el-tab-pane label="视频管理" name="3">
            <studyVideo  :deptId ="clickDeptId"   :subId ="clickSubId"  v-if="drawer"/>
          </el-tab-pane>
          <el-tab-pane label="课件管理" name="4">
            <studyFile :deptId ="clickDeptId"   :subId ="clickSubId"  v-if="drawer"/>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-drawer>

  </div>
</template>

<script>
import { listSubject, getSubject, delSubject, addSubject, updateSubject } from "@/api/course/subject";
import { deptTreeSelect } from "@/api/course/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import uploadpic from "./uploadpic";
import chapter from "./chapter";
import task from "./task";
import studyVideo from "./video";
import studyFile from "./file";
import bank from "../question/bank";


export default {
  name: "Subject",
  components: { Treeselect, uploadpic, chapter, bank, task, studyVideo, studyFile },
  dicts: ['sys_normal_disable', 'subjec_type'],
  data() {
    return {
      title:'',
      clickSubId :undefined ,
      clickDeptId :undefined ,
      activeName: '1',
      drawer: false,
      direction: 'rtl',
      content: null,
      editorOption: {},
      // 部门树选项
      deptOptions: [],
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      subjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        subName: null,
        courseId: null,
        detailUrl: null,
        picUrl: null,
        attribute: null,
        stock: null,
        price: null,
        preferentialPrice: null,
        type: null,
        endTime: null,
        productDetail: null,
        sellNum: null,
        status: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "课程产品名不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "所属课程分类不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "产品市场价格不能为空", trigger: "blur" }
        ],
        preferentialPrice: [
          { required: true, message: "产品优惠价格不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDeptTree();
    this.getList();
  },
  methods: {
    bindCourse(id) { 
      let courseName = '-';
      if (this.deptOptions != null && this.deptOptions[0].children != null) {
        this.deptOptions[0].children.forEach(item => {
          if (id == item.id) {
            courseName = item.label
          }
          if (item.children != null) {
            item.children.forEach(item1 => {
              if (id == item1.id) {
                courseName = item.label + '-' + item1.label
              }
              if (item1.children != null) {
                item1.children.forEach(item2 => {
                  courseName = item.label + '-' + item1.label + '-' + item2.label
                })
              }
            })
          }
        })
      }
      return courseName;
    },
    other(row) { 
      this.clickSubId  = row.id 
      this.clickDeptId = row.courseId;
      this.title  = row.name
      this.drawer = true;

    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    bindPic(url) {
      if (url == null) {
        return require("@/assets/404_images/404.png");
      }
      return process.env.VUE_APP_BASE_API + url;
    },
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      if (
        this.queryParams.courseId == 100
      ) { this.queryParams.courseId = null }
      listSubject(this.queryParams).then(response => {
        this.subjectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        subName: null,
        courseId: null,
        detailUrl: null,
        picUrl: null,
        attribute: null,
        stock: null,
        price: null,
        preferentialPrice: null,
        type: null,
        endTime: null,
        productDetail: null,
        sellNum: null,
        status: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.courseId = this.queryParams.courseId 
      this.form.status = '0'
      this.form.type = '1'
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSubject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSubject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSubject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程编号为"' + ids + '"的数据项？').then(function () {
        return delSubject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/subject/export', {
        ...this.queryParams
      }, `subject_${new Date().getTime()}.xlsx`)
    },
    /** 查询科目分类下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.form.courseId = data.id;
      this.queryParams.courseId = data.id; 
      this.handleQuery();
    },
  }
};
</script>
